/**
 * 统计【玄关】占比
 */
const globalConf = require('../../../config/config.prod.json');
const fs = require('fs-extra');
const path = require('path');
const { generateXLSX } = require('../../utils/tool');
const MongoDatabase = require('../../Mongo/index');
const MysqlDatabase = require('../../Model/index');
const { fetchData, fetchMysqlFloorplanData, fetchMysqlFloorplanImageData } = require('./rule/index');
const TARGET_PATH = path.resolve(__dirname, '../../../output/');

const XLSX_TABLE_HEADER = ['户型图ID', '是否存在玄关', '模型ID', '模型版本', '图片地址'];

const ROOM_TYPE_MAP = {
	XUANGUAN: '玄关',
	// GUODAO: '过道'
};

const defaultMongoSearchOption = {
	startDate: '2024-01-01',
	endDate: '2024-10-10',
	limit: 1000
};

function mapModelIdAndModelVersion(dataList, modelList) {
	return dataList.map((data) => {
		const modelData = modelList.find(item => item.fpid === data.fpid);

		if (modelData) {
			return {
				...data,
				model_id: modelData.modelId,
				model_version: modelData.modelVersion
			};
		}
	});
}

function mapFloorplanColorImage(dataList, imageList) {
	return dataList.map((data) => {
		const imageData = imageList.find(item => {
			return item.fpid === data.fpid;
		});

		if (imageData) {
			return {
				...data,
				url: imageData.url
			};
		}

		return {
			...data,
			url: ''
		};
	});
}

(async () => {
	const { mongo: mongoOptions, mysql: mysqlOptions } = globalConf;
	const searchOptions = globalConf['search-condition']['analize-xuanguan'];

	const MongoModel = await MongoDatabase(mongoOptions);
	const MysqlModel = await MysqlDatabase(mysqlOptions);

	const floorplanList = await fetchData(MongoModel.Drawing, {
		...defaultMongoSearchOption,
		...searchOptions
	});

	const modelDataList = await fetchMysqlFloorplanData(MysqlModel.Model.Floorplan, {
		fpidList: floorplanList.map(item => item.fpid)
	});

	const imageList = await fetchMysqlFloorplanImageData(MysqlModel.Model.FloorplanImage, {
		type: 'COLOR',
		level: 0,
		fpidList: floorplanList.map(item => item.fpid)
	});


	// 数据分析
	floorplanList.forEach(model => {
		const drawing = model.drawing;
		const floorList = drawing.floor.list;

		for (const floor of floorList) {
			const { roomList: roomListExt = [] } = floor;

			const roomIndexList = roomListExt
				.filter(item => Object.values(ROOM_TYPE_MAP).includes(item.name));

			if (roomIndexList.length === 0) {
				model._isValidData = false;

				break;
			}

			model._isValidData = true;
		}
	});

	const quadrilateralFloorplanList = floorplanList.filter(floorplan => floorplan._isValidData);
	const proportion = parseFloat(quadrilateralFloorplanList.length / floorplanList.length) * 100 + '%';

	let dataList = floorplanList.map(data => {
		return {
			fpid: data.fpid,
			is_valid: data._isValidData ? '是' : '否',
			model_id: '',
			model_version: '',
			url: data.url
		};
	});

	dataList = mapModelIdAndModelVersion(dataList, modelDataList);
	dataList = mapFloorplanColorImage(dataList, imageList);

	const _dataList = dataList.map((data) => {
		return Object.values(data).map(iv => {
			if (Array.isArray(iv)) {
				return iv.join(',');
			}

			return iv;
		});
	});

	_dataList.unshift(XLSX_TABLE_HEADER);

	_dataList.unshift([
		`
		统计总数: ${floorplanList.length},
		存在【玄关】占有率: ${proportion}
		`
	]);

	await fs.ensureDir(TARGET_PATH);

	await generateXLSX(_dataList, TARGET_PATH + '/' + `【玄关】占比统计_${Date.now()}.xlsx`);

	process.exit(0);
})();